package com.proyecto.track.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.proyecto.track.bo.Account;

public class AccountDAOImp implements AccountDAO {

private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public Long createAccount(Account account) {
		sessionFactory.getCurrentSession().save(account);
		return account.getId();
	}

	@Override
	public Long deleteAccount(Account account) {
		Long id = account.getId();
		sessionFactory.getCurrentSession().delete(account);
		return id;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Account> searchAccount() {
		return  sessionFactory.getCurrentSession().createQuery("from Account").list();
	}

	@Override
	public Account searchAccount(Long id) {
		return (Account) sessionFactory.getCurrentSession().get(Account.class, id);
	}

	@Override
	public Account searchAccount(String userName) {
		Query query = sessionFactory.getCurrentSession().createQuery("from Account as a where a.userName = :text");
		query.setParameter("text", userName);
		return (Account) query.uniqueResult();
	}
}
